#include "transdialog.h"
#include <QLayout>
#include <QVBoxLayout>

TransDialog::TransDialog(DDialog *parent) : DDialog(parent) {
  setWindowFlag(Qt::WindowStaysOnTopHint);
  setWindowFlag(Qt::Window);
  delete layout();
  auto mlayout = new QVBoxLayout;
  setLayout(mlayout);
  mlayout->setMargin(10);
  setMaximumWidth(400);
  m_word = new DLabel(this);
  mlayout->addWidget(m_word);
  mlayout->addSpacing(5);
  m_phonetic = new DLabel(this);
  mlayout->addWidget(m_phonetic);
  mlayout->addSpacing(5);
  m_content = new DTextBrowser(this);
  m_content->setUndoRedoEnabled(false);
  m_content->setShortcutEnabled(false);
  mlayout->addWidget(m_content);

  setCloseButtonVisible(false);
  setModal(true);
}

void TransDialog::setInfo(QString word, QString ukphonetic, QString usphonetic,
                          QString content) {
  m_word->setText(word);
  if (ukphonetic.length() && usphonetic.length()) {
    m_phonetic->setText(
        QString("[英] %1    [美] %2").arg(ukphonetic).arg(usphonetic));
    m_phonetic->setVisible(true);
  } else {
    m_phonetic->setVisible(false);
  }

  if (content.length()) {
    m_content->setMarkdown(content);
  } else {
    m_content->setText("暂无翻译……");
  }
}

void TransDialog::popup() {
  move(QCursor::pos());
  show();
  raise();
}

void TransDialog::closeEvent(QCloseEvent *event) {
  Q_UNUSED(event);
  hide();
}

void TransDialog::leaveEvent(QEvent *e) {
  Q_UNUSED(e);
  hide();
}

void TransDialog::focusOutEvent(QFocusEvent *event) {
  Q_UNUSED(event);
  hide();
}
